/**
 * Copyright (c) 2011 by Chances.
 * $HeaderURL$:
 * $Author$:
 * $Date$:
 * $Rev$:
 */
package chances.iptv.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

/**
 * Class comments.
 * 
 * @author <a href='mailto:chenxx@chances.com.cn'>Alpha</a>
 * 
 */
public class EPGInfoInterceptor extends HandlerInterceptorAdapter {

    /*
     * (non-Javadoc)
     * 
     * @see
     * org.springframework.web.servlet.handler.HandlerInterceptorAdapter#preHandle
     * (javax.servlet.http.HttpServletRequest,
     * javax.servlet.http.HttpServletResponse, java.lang.Object)
     */
    @Override
    public boolean preHandle(HttpServletRequest req, HttpServletResponse resp,
            Object handler) throws Exception {
        String servletPath = req.getServletPath();
        String flag = req.getParameter("dataInit");
        String page = req.getParameter("page");
        String servletName = servletPath.replace("/", "");
        servletName = servletName.replace(".do", "");
        if (StringUtils.isNotEmpty(flag)) {
            page = "1";
        } else if (StringUtils.isEmpty(page)) {
            page = (String) req.getSession()
                    .getAttribute(servletName + "_page");
        }
        if (StringUtils.isEmpty(page)) {
            page = "1";
        }
        req.setAttribute("page", page);
        req.getSession().setAttribute(servletName + "_page", page);
        String returnURL = req.getParameter("returnURL");
        if (StringUtils.isNotEmpty(returnURL)) {
            req.getSession().setAttribute(servletName + "_return_url",
                    returnURL);
        }
        return super.preHandle(req, resp, handler);
    }

}
